package cn.cvzhanshi.wechatpush.config;


import cn.cvzhanshi.wechatpush.constant.WeChatConstant;
import cn.cvzhanshi.wechatpush.entity.Weather;
import cn.cvzhanshi.wechatpush.utils.DayUtils;
import cn.cvzhanshi.wechatpush.utils.WeChatUtils;
import cn.cvzhanshi.wechatpush.utils.WeatherUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author cVzhanshi
 * @create 2022-08-04 21:09
 */
@Slf4j
public class Pusher {

    public static void main(String[] args) throws WxErrorException {
        sendOne();
        //sendAll();
    }
    /**
     *
     * 发送单用户
     * @author yupeng
     * @date 2022/8/23 14:09
     * @return void
     */
    public static void sendOne() throws WxErrorException {
        String content = WeatherUtils.getRecommend();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String loveDay = WeChatConstant.lianAi_date;
        String birthDay = WeChatConstant.birthday_date;
        String userId =  WeChatConstant.toUserId;
        String cityName = WeChatConstant.cityName;
        Pusher.push(userId,content,loveDay,birthDay,cityName);
    }

    /**
     * 发送所有用户
     * @author yupeng
     * @date 2022/8/23 14:09
     * @return void
     */
    public static void sendAll() throws WxErrorException {
        List<String> openids = Pusher.getOpenids();
        String content = WeatherUtils.getRecommend();
        String loveDay = WeChatConstant.lianAi_date;
        String birthDay = WeChatConstant.birthday_date;
        String cityName = WeChatConstant.cityName;
        openids.stream().forEach(item->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Pusher.push(item,content,loveDay,birthDay,cityName);
        });
    }



    /**
     *
     *  获取所有用户
     * @author yupeng
     * @date 2022/8/23 10:34
     * @return void
     */
    public static List<String> getOpenids() throws WxErrorException {
        WxMpService wxMpService = WeChatUtils.getWxMpService();
        WxMpUserService userService = wxMpService.getUserService();
        WxMpUserList wxMpUserList = userService.userList(null);
        return wxMpUserList.getOpenids();
    }

    /**
     * 推送消息
     * @param toUser
     * @param content
     * @param loveDay
     * @param birthDay
     * @param cityName
     * @author yupeng
     * @date 2022/8/23 14:21
     * @return void
     */
    public static void push(String toUser,String content,String loveDay,String birthDay,String cityName){
        WxMpService wxMpService = WeChatUtils.getWxMpService();
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(toUser)
                .templateId(WeChatConstant.templateId)
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息
        Weather weather = WeatherUtils.getWeather(cityName);
        templateMessage.addData(new WxMpTemplateData("date",weather.getDate() + "  星期"+ weather.getDayOfWeek(),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("city",weather.getAddress() + "","#173177"));
        templateMessage.addData(new WxMpTemplateData("weather",weather.getDayWeather() + "转"+ weather.getNightWeather(),"#EE212D"));
        templateMessage.addData(new WxMpTemplateData("min_temperature",weather.getNightTemp()+ "","#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("max_temperature",weather.getDayTemp()+ "","#42B857" ));
        templateMessage.addData(new WxMpTemplateData("love_day", DayUtils.calculationLianAi(loveDay)+ "","#B95EA3" ));
        templateMessage.addData(new WxMpTemplateData("birthday", DayUtils.calculationBirthday(birthDay)+"","#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("content",content,"#C71585" ));
        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
            log.error("推送失败：",e);
        }
    }
}
